package com.zyx.mybatisdemo.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/30 10:57
 */
public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;

    // 静态代码块 static 特点: 类加载时候执行 只执行一次
    static {
        // 读取配置文件
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            // 创建sqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 提供sqlSession
     * @return 获取的sqlSession
     */
    public static SqlSession getSqlSession(){
        // 获取sqlSession 执行sql语句
        return sqlSessionFactory.openSession();
    }

    /**
     * 关闭sqlSession
     * @param sqlSession 需要关闭的sqlSession
     */
    public static void close(SqlSession sqlSession){
        sqlSession.close();
    }

}
